/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.matrices;

import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.Columna;
import clases.Celda;
import clases.Matriz;
import clases.Usuario;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author andreth
 */
public class AgregarFila extends DispatchAction {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String name = "Agregar Matriz";

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        return mapping.findForward(SUCCESS);
    }

    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Matriz m = (Matriz) form;
        System.out.println("Estoy entrando a guardar las filas");
        System.out.println("Tamano array filas " + m.getFilas().size());

      
        ArrayList<Columna> columnas;

        if ((columnas = dbms.consultarColumnas(m)) != null) {
            System.out.println("Tamano array columnas m y db " + m.getColumnas().size() + " " + columnas.size());
            for (int i = 0; i < columnas.size(); i++) {
                for (int j = 0; j < m.getFilas().size(); j++) {
                    Celda f = m.getFilas().get(j);
                    System.out.println("Fila" + f.getNombre() + f.getIdLogico());

                    if (f.getIdLogico() == i) {
                        Columna c = columnas.get(i);
                        f.setIdColumna(c.getIdColumna());
                        System.out.println("f.getIdColumna" + f.getIdColumna());
                    }
                }
            }
            m.setColumnas(columnas);
        }


        if (dbms.agregarFilas(m)) {
            m.setMensajeOK("Matriz agregada");
            return mapping.findForward(SUCCESS);
        } else {
            m.setMensaje("Matriz no agregada");
            return mapping.findForward(FAILURE);
        }

    }

    public ActionForward addFila(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        
        Matriz m = (Matriz) form;
        m.agregarFila();
        return mapping.findForward(SUCCESS);
    }
}
